﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using Resn.WhuPub.BLL;
using Resn.WhuPub.DAL;
using Resn.WhuPub.DML;

namespace Resn.WhuPub.BLL
{
        /// <summary>
        /// 实现抽象实体对象的属性和方法
        /// </summary>
        public abstract class Entity<M>: NotifyPropertyChangedObject ,IIdentity where M:class ,IIdentity
        {
                #region 构造函数

                /// <summary>
                /// 实体构造函数
                /// </summary>
                /// <param name="model"></param>
                public Entity(M model)
                {
                        Model = model;
                }

                #endregion


                /// <summary>
                /// 获取标识
                /// </summary>
                /// <value>
                /// 标识
                /// </value>
                public string Id
                {
                        get 
                        {
                                return Model.Id;
                        }
                }

                /// <summary>
                /// 获取或设置数据模型
                /// </summary>
                /// <value>
                /// 数据模型
                /// </value>
                public M Model { get; protected set; }

                /// <summary>
                /// 添加对象到数据库
                /// </summary>
                public virtual void Insert()
                {
                        DBStorage.Instance.Insert(Model);
                }

                /// <summary>
                /// 删除对象从数据库
                /// </summary>
                public virtual void Delete()
                {
                        DBStorage.Instance.Delete(Model);
                }

                /// <summary>
                /// 更新对象到数据库
                /// </summary>
                public virtual void Update()
                {
                        DBStorage.Instance.Update(Model);
                }

                /// <summary>
                /// 获取
                /// </summary>
                /// <param name="id">The id.</param>
                /// <returns></returns>
                public static E Select<E>(string id)
                        where E:Entity<M>
                {
                        var model = DBStorage.Instance.Select<M>(id);
                        return Activator.CreateInstance(typeof(E), model) as E;

                        
                }
        }
}
